﻿/*
Copyright (c) 2010 Lance Ward
This software is licensed under the MIT license, except where otherwise noted.
For complete license details, refer to license.txt
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using ADNLib;

namespace ADNActions
{
	public enum MirrorDirection
	{
		Right,
		Left,
		Top,
		Bottom
	}

	public static class Rotate
	{
		public static void FlipHoriz(List<XObject> selection)
		{
			if (selection != null)
			{
				RectangleF groupBounds = GetBounds(selection);

				for (int i = 0; i < selection.Count; i++)
				{
					selection[i].FlipHoriz((groupBounds.Left * 2) + groupBounds.Width);
				}
			} 
		}

		public static void FlipVert(List<XObject> selection)
		{
			if (selection != null)
			{
				RectangleF groupBounds = GetBounds(selection);

				for (int i = 0; i < selection.Count; i++)
				{
					selection[i].FlipVert((groupBounds.Top * 2) + groupBounds.Height);
				}
			}
		}

        //TODO: move to shared loc. this is a DUPLICATE of the XGroup function

        public static RectangleF GetBounds(List<XObject> objs)
        {
            //loop through children., getting bounds. create max rect
            RectangleF maxRect = RectangleF.Empty;
            for (int i = 0; i < objs.Count; i++)
            {
                if (i == 0) { maxRect = objs[i].Bounds; }
                else
                {
                    maxRect = RectangleF.Union(objs[i].Bounds, maxRect);
                }
            }
            return maxRect;
        }

        public static void MirrorHoriz(List<XObject> objects, List<XObject> selection, MirrorDirection dir)
		{
			//clone and flip along the horizontal <--->
            if (selection != null)
            {
                List<XObject> clones = new List<XObject>();

                RectangleF groupBounds = GetBounds(selection);

                for (int i = 0; i < selection.Count; i++)
                {
                    XObject o = (XObject)AnimIO.CloneObject(selection[i]);
                    clones.Add(o);
					float offset = (groupBounds.Left * 2);
					if (dir == MirrorDirection.Right) { offset += (groupBounds.Width * 2); }
                    o.FlipHoriz(offset);
                }

                objects.AddRange(clones);
            } 
		}

        public static void MirrorVert(List<XObject> objects, List<XObject> selection, MirrorDirection dir)
		{
			//clone and flip along the vertical  | 
			//                                   \/
            if (selection != null)
            {
                List<XObject> clones = new List<XObject>();

                RectangleF groupBounds = GetBounds(selection);

                for (int i = 0; i < selection.Count; i++)
                {
                    XObject o = (XObject)AnimIO.CloneObject(selection[i]);
                    clones.Add(o);
					float offset = (groupBounds.Top * 2);
					if (dir == MirrorDirection.Bottom) { offset += (groupBounds.Height * 2); }
					o.FlipVert(offset);
                }

                objects.AddRange(clones);
            } 
		}

		public static void Rotate90(List<XObject> objects, List<XObject> selection)
		{
            for (int i = 0; i < selection.Count; i++)
            {
                XObject o = selection[i].ConvertToPath();
                if (o != null) 
                {
                    int index = objects.IndexOf(selection[i]);
                    selection[i] = o;
                    objects[index] = o;
                }

                selection[i].Rotate(90.0f);
            }
		}

        public static void Rotate90Rev(List<XObject> objects, List<XObject> selection)
		{
            for (int i = 0; i < selection.Count; i++)
            {
                XObject o = selection[i].ConvertToPath();
                if (o != null)
                {
                    int index = objects.IndexOf(selection[i]);
                    selection[i] = o;
                    objects[index] = o;
                }

                selection[i].Rotate(-90.0f);
            }
		}

        //TODO: move this to the pointer tool. Rotate based on the angle of click point to center.
        //call with a virtual line from the center point of the rectangle
        //to the clicked point to calculate the angle

    }
}
